package com.nhjf.dao.finetTalk;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.nhjf.dao.BaseDao;
import com.nhjf.model.finetTalk.FinetTalk;
import com.nhjf.pojo.qo.finetTalk.FinetTalkQo;

@Repository
public class FinetTalkDao extends BaseDao<FinetTalk,FinetTalkQo> {

	@Override
	protected Criteria buildCriteria(Criteria criteria, FinetTalkQo qo) {
		// TODO Auto-generated method stub
		if(qo!=null) {	
			criteria.setFetchMode("publisher", qo.isSeachfinetTalkid()?FetchMode.JOIN:FetchMode.SELECT);	
			if(qo.getFinetTalkid()!=null) {
				criteria.add(Restrictions.eq("finetTalkid", qo.getFinetTalkid()));
			}
			//导航分类
			if(qo.getNavigation()!=null) {
				criteria.add(Restrictions.eq("navigation", qo.getNavigation()));
			}
			//封面
			if(StringUtils.isNotBlank(qo.getImge())) {
				criteria.add(Restrictions.eq("imge", qo.getImge()));
			}
			//置顶
			if(qo.getTopping()!=null) {
				criteria.add(Restrictions.eq("topping", qo.getTopping()));
			}
			//浏览量
			if(qo.getPageviews()!=null) {
				criteria.add(Restrictions.eq("pageviews", qo.getPageviews()));
			}
			//标题
			if(StringUtils.isNotBlank(qo.getTitle())) {
				criteria.add(Restrictions.like("title", qo.getTitle(),MatchMode.ANYWHERE));
			}
			//发布时间
			if(qo.getReleasetime()!=null) {
				criteria.add(Restrictions.eq("releasetime", qo.getReleasetime()));
			}
			//发布人
			if(qo.getPublisher()!=null) {
				criteria.add(Restrictions.eq("publisher.id", qo.getPublisher()));
			}
			if(StringUtils.isNotBlank(qo.getNiciName())) {
				 criteria.createAlias("publisher", "publisher");
				 criteria.add(Restrictions.like("publisher.nickname", qo.getNiciName(),MatchMode.ANYWHERE));
			}
			//发布内容
			if(StringUtils.isNotBlank(qo.getContent())) {
				criteria.add(Restrictions.eq("content", qo.getContent()));
			}
			if(qo.getSerialNumber()!=null) {
				criteria.add(Restrictions.eq("serialNumber", qo.getSerialNumber()));
			}
			criteria.addOrder(Order.desc("topping"));
			criteria.addOrder(Order.desc("serialNumber"));
			criteria.addOrder(Order.desc("releasetime"));
		}
		return criteria;
	}

	@Override
	protected Class<FinetTalk> getEntityClass() {
		// TODO Auto-generated method stub
		return FinetTalk.class;
	}

	

}
